package com.itc.bpm.framework.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.bpm.framework.enums.BpmComment;
import com.itc.bpm.framework.factory.BpmServiceFactory;
import com.itc.bpm.framework.factory.vo.BpmQueryVo;
import com.itc.bpm.framework.factory.vo.BpmTaskVo;
import com.itc.bpm.framework.factory.vo.dto.BpmTaskDto;
import com.itc.bpm.framework.factory.vo.dto.FormTemplate;
import com.itc.bpm.framework.factory.vo.dto.TaskDto;
import com.itc.bpm.framework.factory.vo.task.BpmTaskApproveReqVO;
import com.itc.bpm.framework.factory.vo.task.BpmTaskTodoItemRespVO;
import com.itc.bpm.framework.service.TaskExtService;
import com.itc.common.core.domain.R;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.task.api.DelegationState;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
@Service
public class TaskServiceExtImpl extends BpmServiceFactory implements TaskExtService {
    @Override
    public List<FormTemplate> getFormTemplates() {
        return null;
    }

    @Override
    public R complete(BpmTaskVo taskVo) {
        Task task = taskService.createTaskQuery().taskId(taskVo.getTaskId()).singleResult();
        //pending标识尚未开始或者正在执行
        if(task==null)
            return R.fail("任务不存在");
        //throw new BusException("", BaseResultCode.GENERAL_ERROR);
        if (DelegationState.PENDING.equals(task.getDelegationState())) {
            taskService.addComment(task.getId(), task.getProcessInstanceId(), BpmComment.DELEGATE.getType(), taskVo.getComment());
            taskService.resolveTask(taskVo.getTaskId(), taskVo.getVariables());
        } else {
            taskService.addComment(task.getId(), task.getProcessInstanceId(), BpmComment.NORMAL.getType(), taskVo.getComment());
            //TODO:后期从登录账号中获取登录人信息
            taskService.setAssignee(task.getId(), taskVo.getUserId()==null?"yangli":taskVo.getUserId());
            taskService.complete(task.getId(), taskVo.getVariables());
            //taskService.complete(task.getId());
        }
        return R.ok();
    }

    @Override
    public void taskReject(BpmTaskVo flowTaskVo) {

    }

    @Override
    public void taskReturn(BpmTaskVo flowTaskVo) {

    }

    @Override
    public R findReturnTaskList(BpmTaskVo flowTaskVo) {
        return null;
    }

    @Override
    public void deleteTask(BpmTaskVo flowTaskVo) {

    }

    /**
     * 认领任务，设置操作人
     * @param taskId
     */
    @Override
    public void claim(String taskId,String userId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if(task!=null){
            //设置审批操作人
            taskService.setAssignee(taskId,userId);
        }
    }

    @Override
    public void unClaim(BpmTaskVo flowTaskVo) {

    }

    @Override
    public void delegateTask(BpmTaskVo flowTaskVo) {

    }

    @Override
    public void resolveTask(BpmTaskVo flowTaskVo) {

    }

    @Override
    public void assignTask(BpmTaskVo flowTaskVo) {

    }

    @Override
    public void addMultiInstanceExecution(BpmTaskVo flowTaskVo) {

    }

    @Override
    public void deleteMultiInstanceExecution(BpmTaskVo flowTaskVo) {

    }

    @Override
    public R myProcess(BpmQueryVo queryVo) {
        return null;
    }

    @Override
    public R stopProcess(BpmTaskVo flowTaskVo) {
        return null;
    }

    @Override
    public R revokeProcess(BpmTaskVo flowTaskVo) {
        return null;
    }

    @Override
    public R todoList(BpmQueryVo queryVo) {
        return null;
    }

    @Override
    public R finishedList(BpmQueryVo queryVo) {
        return null;
    }

    @Override
    public R flowRecord(String procInsId) {
        return null;
    }

    @Override
    public InputStream diagram(String processId) {
        return null;
    }

    @Override
    public R getFlowViewer(String procInsId, String executionId) {
        return null;
    }

    @Override
    public R processVariables(String taskId) {
        return null;
    }

    @Override
    public R flowFormData(String deployId) {
        return null;
    }

    @Override
    public R flowXmlAndNode(String procInsId, String deployId) {
        return null;
    }

    @Override
    public R flowTaskForm(String taskId) throws Exception {
        return null;
    }

    @Override
    public R todoProcessInstanceList(String processInstaneId) {
        return null;
    }

    @Override
    public R getProcessAllTasks(String processInstaneId) {
        return null;
    }

    @Override
    public String getNodeNameByKey(String key) {
        return null;
    }

    @Override
    public void addVaribals(Map<String, Object> map, String taskId) {

    }

    @Override
    public String getTaskName(String taskId) {
        return null;
    }

    @Override
    public List<TaskDto> findTaskIdNextNode(String processInstanceId, String taskSId) {
        return null;
    }

    @Override
    public List<TaskDto> findTaskIdStartNode(String processInstanceId, String taskId) {
        return null;
    }

    @Override
    public R<Page<BpmTaskTodoItemRespVO>> todoPageList(BpmQueryVo taskReqVo) {
        return null;
    }

    @Override
    public void approveTask(String userId, BpmTaskApproveReqVO reqVO) {

    }

    @Override
    public R<List<TaskDto>> getTaskInfo(String definitionId) {
        return null;
    }

    @Override
    public List<BpmTaskDto> findActivitiOrFinishTaskResult(String orderProcessId) {
        return null;
    }

    @Override
    public R getProcessAllTaskINfo(String processId) {
        return null;
    }

    @Override
    public R getProcessTaskINfo(String taskId) {
        return null;
    }

    @Override
    public List<BpmTaskTodoItemRespVO> exportList(BpmQueryVo taskReqVo) {
        return null;
    }

    @Override
    public R getTaskFormData(String serviceType, Long serviceId) {
        return null;
    }

    @Override
    public List<BpmTaskTodoItemRespVO> taskExportList(BpmQueryVo taskReqVo) {
        return null;
    }

    @Override
    public List<HistoricTaskInstance> findHistoricTaskInstance(String processInstanceId) {
        return  historyService.createHistoricTaskInstanceQuery()
                .processInstanceId(processInstanceId)
                .orderByHistoricTaskInstanceStartTime().desc() // 创建时间倒序
                .list();
    }

    @Override
    public List<HistoricActivityInstance> findHistoricActivityInstance(String processInstanceId) {
        return historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId).list();
    }
}
